package com.example.yuejian.yuejian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.adapter.ShowCommnetAdapter;
import com.example.yuejian.yuejian.base.BaseActivity;
import com.example.yuejian.yuejian.base.ModleListListener;
import com.example.yuejian.yuejian.base.ModleListener;
import com.example.yuejian.yuejian.bean.AddComment;
import com.example.yuejian.yuejian.bean.CommentList;
import com.example.yuejian.yuejian.bean.ShowPhotoDetail;
import com.example.yuejian.yuejian.model.CommentModel;
import com.example.yuejian.yuejian.model.ShowPhotoModel;
import com.example.yuejian.yuejian.view.RoundImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

public class ShowPhotoDetailActivity extends BaseActivity {
   private TextView show_detail_username;
    private RoundImageView show_detail_userimage;
    private TextView show_detail_clubname;
    private ImageView show_detail_photo;
    private TextView show_detail_title;

    private EditText comment_content;
    private Button btn_commnet;

    private ImageView btn_back;
    private RecyclerView recyclerView;

    String showid;

    private ModleListener<ShowPhotoDetail>photoDetailModleListener=new ModleListener<ShowPhotoDetail>() {
        @Override
        public void onResponse(ShowPhotoDetail o) {
            ShowPhotoDetail res=o;
            show_detail_username.setText(res.getUsername());
            if (!"".equals(res.getUserphoto()))
            {
                Picasso.with(getApplication()).load(res.getUserphoto()).into(show_detail_userimage);
            }
            else
            {
                show_detail_userimage.setImageResource(R.mipmap.ic_launcher);
            }
            show_detail_clubname.setText("#"+res.getClubname()+"#");
            if (!"".equals(res.getPhoto()))
            {
                Picasso.with(getApplication()).load(res.getPhoto()).into(show_detail_photo);
            }

            show_detail_title.setText(res.getTitle());
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private ModleListListener<CommentList>commentListModleListListener=new ModleListListener<CommentList>() {
        @Override
        public void onResponse(List<CommentList> l) {
            List<CommentList> list=l;
            recyclerView.setAdapter(new ShowCommnetAdapter(getApplication(),list));

        }

        @Override
        public void onFail(String msg) {

        }
    };

    private ModleListener<AddComment>addCommentModleListener=new ModleListener<AddComment>() {
        @Override
        public void onResponse(AddComment o) {
            AddComment res=o;
            Toast.makeText(getApplication(),res.getRetMsg(),Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplication(),"cuowo",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_photo_detail);
        initViews();
        initEvents();
        back();
    }
    private void initViews()
    {
        show_detail_username= (TextView) findViewById(R.id.show_detail_username);
        show_detail_userimage= (RoundImageView) findViewById(R.id.show_detail_userImage);
        show_detail_clubname= (TextView) findViewById(R.id.show_detail_clubname);
        show_detail_photo= (ImageView) findViewById(R.id.show_detail_showImage);
        show_detail_title= (TextView) findViewById(R.id.show_detail_title);
        comment_content= (EditText) findViewById(R.id.et_show_detail_comment);
        btn_commnet=(Button)findViewById(R.id.show_detail_commite_comment_btn);

        recyclerView=(RecyclerView)findViewById(R.id.id_showpoto_detail_recycleview);
        // GridLayoutManager gm = new GridLayoutManager(this.getActivity(),2);
        // 网格是的list布局   2列
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        //recyclerView.setLayoutManager(gm);
    }
    private void initEvents()
    {
           btn_commnet.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                   add();
                   getData();
               }
           });
    }
    private  void getData()
    {
        Intent intent=getIntent();
        showid=intent.getStringExtra("showphoto_id");
        ShowPhotoModel model=new ShowPhotoModel();
        model.showPhotodetail(showid,photoDetailModleListener);

        CommentModel model1=new CommentModel();
        model1.Commentlist(showid,commentListModleListListener);
    }
    private void add()
    {
        CommentModel model=new CommentModel();
        model.AddCommnet(getUser_id(),comment_content.getText().toString(),showid,addCommentModleListener);
    }

    private void back()
    {
        btn_back=(ImageView)findViewById(R.id.imgBtn_show_photo_detail_detail_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }
    @Override
    protected void onResume() {
        getData();
        super.onResume();
    }
}
